//
//  ZXLNetWorking.m
//  ZXLSign
//
//  Created by zxl02 on 2020/11/10.
//

#import "ZXLNetWorking.h"
#import "ZXLSessionManager.h"
#import "AFHTTPSessionManager+Extension.h"
@interface ZXLNetWorking ()
@end
//static AFHTTPSessionManager *manager;
static ZXLSessionManager *manager;

@implementation ZXLNetWorking
+ (ZXLSessionManager *)sessionManager
{
    manager = [ZXLSessionManager manager];
    manager.requestSerializer.timeoutInterval = 60;
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    return manager;
}
+ (NSURLSessionDataTask *)getWithURLString:(NSString *)urlString
                                parameters:(id)parameters
                                   success:(SuccessBlock)successBlock
                                   failure:(FailureBlock)failureBlock
{
    ZXLSessionManager *manage = [self sessionManager];
    [manage addsign];
//    [manage addSign];
//    if ([LoginInfoHelper isSecurity]) {
//        [self setSecurityHeader:manage];
//    }
//    NSString * URLStr = [NSString stringWithFormat:@"%@?%@",urlString,[self getUE:YES]];
//    [self setV2SecurityHeader:manage url:urlString body:parameters requestType:@"GET"];//v2
//    NSLog(@"getUE = %@ getURL= %@   GET",[self getUE:NO],urlString);
    return
    [manage GET:urlString parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"正确 %@",responseObject);
        if (responseObject[@"_req_id"]) {
                 
//                 responseObject = [[ZXLNetMixAlgorithm sharedInstance] decryptParams:responseObject];
            }
            if (successBlock) {
                successBlock(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"错误 %@",error);
            if (failureBlock) {
                failureBlock(error);
                NSLog(@"GET网络异常 - T_T%@", error);
            }
        }];
}

/*+ (AFHTTPSessionManager *)sessionManager
{
    manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 60;
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    return manager;
}
+ (NSURLSessionDataTask *)getWithURLString:(NSString *)urlString
                                parameters:(id)parameters
                                   success:(SuccessBlock)successBlock
                                   failure:(FailureBlock)failureBlock
{
    AFHTTPSessionManager *manage = [self sessionManager];
//    if ([LoginInfoHelper isSecurity]) {
//        [self setSecurityHeader:manage];
//    }
//    NSString * URLStr = [NSString stringWithFormat:@"%@?%@",urlString,[self getUE:YES]];
//    [self setV2SecurityHeader:manage url:urlString body:parameters requestType:@"GET"];//v2
//    NSLog(@"getUE = %@ getURL= %@   GET",[self getUE:NO],urlString);
    return
    [manage GET:urlString parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
             if (responseObject[@"_req_id"]) {
//                 responseObject = [[ZXLNetMixAlgorithm sharedInstance] decryptParams:responseObject];
            }
            if (successBlock) {
                successBlock(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failureBlock) {
                failureBlock(error);
                NSLog(@"GET网络异常 - T_T%@", error);
            }
        }];
}

+ (NSURLSessionDataTask *)postWithURLString:(NSString *)urlString
                                 parameters:(id)parameters
                                    success:(SuccessBlock)successBlock
                                    failure:(FailureBlock)failureBlock
{
    
    AFHTTPSessionManager *manage = [self sessionManager];
    return
        [manage POST:urlString parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (responseObject[@"_req_id"]) {
//                responseObject = [[ZXLNetMixAlgorithm sharedInstance] decryptParams:responseObject];
            }
           if (successBlock) {
             successBlock(responseObject);
           }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failureBlock) {
                failureBlock(error);
                NSLog(@"POST网络异常 - T_T%@", error);
            }
        }];
}

-(void)getSignWithManagerHeader:(AFHTTPSessionManager*)manager UrlString:(NSString*)urlString  parameters:(id)parameters requestType:(NSString*)type{
    ZXLAPISignData * data =[ZXLAPISignData new];
    data.URL=urlString;
    data.queryString=parameters;
    data.body=parameters;
    data.requestType=type;
    [[ZXLAPI new] setManager:manager SignWith:data];
}

-(void)AspectsMethond{
    [AspectsTool zxl_AspectsWithClassName:@"AFHTTPSessionManager" Selector:@selector(POST:parameters:headers:progress:success:failure:) Options:(ZXLAspectPositionBefore) UsingBlock:^(AspectsTool * _Nonnull info, NSError * _Nonnull error) {
            
    }];
}*/
@end
